/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.itschapala.com.dsw2.vista;

import java.util.List;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
import mx.itschapala.com.dsw2.Bl.AdministradorBlLocal;
import mx.itschapala.com.dsw2.modelo.Administrador;

/**
 *
 * @author z
 */
@ManagedBean
@SessionScoped
public class AdministradorBean {
    
       //----- EJBS---//
    
    @EJB
    private AdministradorBlLocal administradorBl;

    
//------------variables----------------
  private List<Administrador> lista;
  private Administrador administra;
  private String accion;
//-------------constructor-------------
 public AdministradorBean() {
    administra = new  Administrador();
    }
 
 //--------------get y set--------------

   public Administrador getAdministra() {
        return administra;
    }

//    public void setAdministra(Administrador administra) {
//        this.administra = administra;
//    }

    public List<Administrador> getLista() {
        lista =administradorBl.getAll();
        System.out.println(lista);
        return lista;
    }

    public void setLista(List<Administrador> lista) {
        this.lista = lista;
    }
  
 //-------------acciones-----------------

    
    public String relizarAccion(){
    if(accion.equalsIgnoreCase("nuevo")){
    administradorBl.register(administra);
    }else{
    if(accion.equalsIgnoreCase("Editar")){
     administradorBl.modify(administra);
    }else{
    if(accion.equalsIgnoreCase("Eliminar")){
        administradorBl.delete(administra);
        }
    }
    }
    accion="";
    return "AdministradorLista";

}    
 
//-------------listeners----------------
    
    
public void preparaNuevo(ActionEvent e){
    administra=new Administrador(); 
    accion="Nuevo";
}
public void preparaEditar(ActionEvent evt){
       System.out.println(evt.getComponent().getClientId());
       int index = Integer.parseInt(evt.getComponent().getClientId().split(":")[2]);
       System.out.println("el numero es:"+ index);
       administra=lista.get(index);
       accion="Editar";               
}
public void preparaEliminar(ActionEvent ev){
    int index = Integer.parseInt(ev.getComponent().getClientId().split(":")[2]);      
       administra=lista.get(index);
accion="Eliminar";
}
   
}

 

